import Vue from 'vue'
import vuex from 'vuex'
import {Base64} from 'js-base64'
import KEYS from './keys'
import REQUEST from './../services/test'
import URL from './../utility/url'

Vue.use(vuex)

export default new vuex.Store({
  state: {
    fetchLoading: false,
    isHeader: false,
    userInfo: {},
    allFactors: [],
    selectFactors: [],
    // hotNews: [],
    // newFactors: [],
    // hotFactors: [],
    strategys: []
  },
  getters: {
    fetchLoading: state => state.fetchLoading
  },
  mutations: {
    getAllFactors (state) {
      let store = localStorage.getItem(KEYS.ALLFACTORS)
      if (store) {
        let _all = JSON.parse(store)
        _all.forEach((v) => {
          v.isSelect = false
        })
        state.allFactors = _all
      } else {
        state.allFactors = []
      }
    },
    getUrlParams (state) {
      let store = window.localStorage.getItem(KEYS.PARAMS_INFO)
      if (store && store !== 'null') {
        let info = JSON.parse(store)
        if (Number(info.hideFlag) === 0) {
          state.isHeader = true
        } else {
          state.isHeader = false
        }
        if(info.params){
          state.userInfo = JSON.parse(Base64.decode(info.params))
        }
      }
    },
    setHotFactors(state, data) {
      console.log('setHotFactors', state)
      state.hotFactors = data || []
    },
    pushSelectFactor(state, val) {
      if (state.selectFactors.length > 0) {
        let a = state.selectFactors.filter(v => {
          return v.factor_id === val.factor_id
        })
        if (a.length <= 0) {
          state.selectFactors.push(val)
        }
      } else {
        state.selectFactors.push(val)
      }
    },
    removeSelectFactor(state, val) {
      state.selectFactors.forEach((item, index) => {
        if (item.factor_id === val.factor_id) {
          state.selectFactors.splice(index, 1)
        }
      })
    },
    clearSelectFactor(state) {
      state.selectFactors.forEach((item, index) => {
        item.isSelect = false
      })
      state.selectFactors = []
    },
    pushStrategy(state, obj) {
      console.log(obj)
      // state.strategys.push(val)
      let arr = []
      let store = localStorage.getItem(KEYS.STRATEGYS)
      if (store) {
        arr = JSON.parse(store)
        let a = arr.filter(v => {
          return v.create_time === obj.create_time
        })
        if (a <= 0) {
          arr.push(obj)
          localStorage.setItem(KEYS.STRATEGYS, JSON.stringify(arr))
        }
      } else {
        arr.push(obj)
        localStorage.setItem(KEYS.STRATEGYS, JSON.stringify(arr))
      }
    },
    removeStrategy(state, obj) {
      console.log('yyy', obj)
      let arr = []
      let store = localStorage.getItem(KEYS.STRATEGYS)
      if (store) {
        arr = JSON.parse(store)
        let a = arr.filter(v => {
          return !(v.create_time === obj.create_time)
        })
        localStorage.setItem(KEYS.STRATEGYS, JSON.stringify(a))
      }
    },
    fetchLoading (state, res) {
      state.fetchLoading = res
    }
  },
  actions: {
    // --缓存所有因子---001
    setAllFactors({commit}) {
      REQUEST.getAllFactors().then(res => {
        // let store = localStorage.getItem(KEYS.ALLFACTORS)
        console.info('-----AllFactors------length-------', res.length)
        if (res.length > 0) {
          window.localStorage.setItem(KEYS.ALLFACTORS, JSON.stringify(res.data))
          commit('getAllFactors')
        }
      }).catch(err => {
        console.info(err)
      })
    },
    // 获取url 参数（长城）
    urlParams({commit, dispatch}) {
      let _url = window.location.href
      let test = {
        hideFlag: 1,
        czqd: 'CCY',
        params: 'eyJtb2JpbGVjb2RlIjoiMTM4MDAxMzgwMDAiLCJ0enR1bmlxdWVpZCI6IjcwQjgyNEFFLTRGNDMtNEU1OC1CRDYzLUI0OEMyRDQxNEEyRSIsImFjY291bnQiOiIxMDAwMDAwMDIyMiJ9'
      }
      let _params = URL.getParams(_url) || test
      window.localStorage.setItem(KEYS.PARAMS_INFO, JSON.stringify(_params))
      commit('getUrlParams')
    },
    // 检测allFactors是否为空
    isAllFactors(context) {
      let _allFactors = context.state.allFactors
      if (_allFactors.length <= 0) {
        console.log('--allFactors=0---')
        context.commit('getAllFactors')
      }
    },

    fetchLoading({commit}, res) {
      commit('fetchLoading', res)
    }
  }
})
